package com.itheima.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

/**
 * 工具类
 *  目标提供jedis连接对象
 */
public class JedisUtils {

    //定义池对象
    private static JedisPool pool = null;

    static{
        /**
         * ResourceBundle 专门用来操作properties
         *   ResourceBundle.getBundle("jedis"); 获得配置文件 (此处不需要后缀)
         */
        //读取配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");

        int minIdle= Integer.valueOf( bundle.getString("jedis.minIdle") );
        int maxWaitMillis = Integer.valueOf( bundle.getString("jedis.maxWaitMillis") );
        int maxTotal = Integer.valueOf( bundle.getString("jedis.maxTotal") );
        int port = Integer.valueOf( bundle.getString("jedis.port") );
        String host = bundle.getString("jedis.host");

        //创建配置
        JedisPoolConfig config = new JedisPoolConfig();
        //config.setMinIdle(3); //最小空闲数
        //config.setMaxWaitMillis(3000); //最大连接时间
        //config.setMaxTotal(30);//最大个数


        //1.创建池
        pool = new JedisPool( config,host ,port );
    }
    /**
     * 提供jedis连接对象
     * @return
     */
    public static Jedis getJedis(){
        return pool.getResource();
    }

    public static void main(String[] args) {
        Jedis jedis = JedisUtils.getJedis();
        System.out.println(jedis);
    }
}
